package by.dvrudenko.avtobaza.webapp.page.order.panel;

import javax.inject.Inject;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.dvrudenko.avtobaza.datamodel.City;
import by.dvrudenko.avtobaza.datamodel.Route;
import by.dvrudenko.avtobaza.datamodel.UserOrder;
import by.dvrudenko.avtobaza.services.RouteService;
import by.dvrudenko.avtobaza.webapp.page.AbstractPanel;
import by.dvrudenko.avtobaza.webapp.page.city.CreateCityPage;
import by.dvrudenko.avtobaza.webapp.page.createOrder.createOrderPage;
import by.dvrudenko.avtobaza.webapp.page.login.component.LoginLogoutPanel;
import by.dvrudenko.avtobaza.webapp.page.order.OrderPage;
import by.dvrudenko.avtobaza.webapp.utils.security.SecuredLinkForAdmin;
import by.dvrudenko.avtobaza.webapp.utils.security.SecuredLinkForUser;

public class OrderDetailsPanel extends AbstractPanel {
	
	@Inject
	private RouteService routeService;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(OrderDetailsPanel.class);
	
	private static final String PATH_CARGO_IMG = "images/order/cargo.png";
	
	Route randomRoute = routeService.getRandomActiveRoute();

	public OrderDetailsPanel(String id) {
		super(id);
	}

	@Override
	protected void onInitialize() {
		super.onInitialize();
		// панель для вывода случайного заказа
		WebMarkupContainer liContainer = new WebMarkupContainer("wrapper-li");
		add(liContainer);
		WebMarkupContainer hContaiener = new WebMarkupContainer("wrapper-h");
		hContaiener.add(new Label("panelOrderTitle", getResourceModel("p.home.order.panel.orderPanelTitle")));
		liContainer.add(hContaiener);
		// add img
		WebMarkupContainer imgContaiener = new WebMarkupContainer("wrapper-img");	
		imgContaiener.add(AttributeModifier.append("src", PATH_CARGO_IMG));
		liContainer.add(imgContaiener);

		liContainer.add(new Label("orderBiginningPoint", new Model<String>(randomRoute.getUserOrder().getCargoBeginningCity().getCityName())));
		liContainer.add(new Label("orderFinalPoint", new Model<String>(randomRoute.getUserOrder().getCargoFinalCity().getCityName())));
		liContainer.add(new Label("orderCargoWeight", new Model<Integer>(randomRoute.getUserOrder().getCargoWeight())));
		liContainer.add(new Label("orderCargoVolume", new Model<Integer>(randomRoute.getUserOrder().getCargoVolume())));
		liContainer.add(new Label("orderCurrentStatus", new ResourceModel(randomRoute.getUserOrder().getOrderStatus().name())));

		// ссылка для страницы с заказами
		liContainer.add(new SecuredLinkForUser("orderPage") {
			@Override
			public void onClick() {
				setResponsePage(OrderPage.class);
			};
		});
		
		// ссылка для создания заказа
		Link<Void> createOrder = new Link <Void>("createOrderPanel"){			
			@Override
			public void onClick() {
				LOGGER.info("user crete UserOrder");
				UserOrder userOrder = new UserOrder();
				setResponsePage(new createOrderPage(userOrder));
			};
		};	
		liContainer.add(createOrder);
		
		// ссылка для создания города
		Link<Void> createSity = new SecuredLinkForAdmin("createSity") {			
			@Override
			public void onClick() { 
				City city = new City();
				setResponsePage(new CreateCityPage(city));
			};
		};		
		liContainer.add(createSity);
		
	};
	
}
